package web.reclamos.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import web.reclamos.beans.Coorporacion;
import web.reclamos.beans.Area;
import web.reclamos.beans.Empresa;
import web.reclamos.service.AreaService;

public class AreaServlet extends HttpServlet {


    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if(request.getServletPath().equals("/area_nuevo.reclamo")) {
            nuevoArea(request, response);
        }else if(request.getServletPath().equals("/area_modificar.reclamo")) {
            modificarArea(request, response);
        }else if(request.getServletPath().equals("/area_eliminar.reclamo")) {
            eliminarArea(request, response);
        }else if(request.getServletPath().equals("/area_consultar.reclamo")) {
            consultarArea(request, response);
        }
    }

    private void nuevoArea(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
          AreaService areService = new AreaService();
          Area area = new Area();
            System.out.println("+++++++++++++++++++++++++++");
            System.out.println(request.getParameter("txtId"));
            System.out.println(request.getParameter("txtNombre"));
            System.out.println(request.getParameter("txtIdEmpresa"));
            System.out.println(request.getParameter("txtIdCoorporacion"));
            System.out.println("+++++++++++++++++++++++++++");
          area.setId_area(Integer.parseInt(request.getParameter("txtId")));
          area.setEmpresa(new Empresa(Integer.parseInt(request.getParameter("txtIdEmpresa")), null, null, null));
          area.setCoorporacion(new Coorporacion(Integer.parseInt(request.getParameter("txtIdCoorporacion")), null, null, null));
          area.setNombre_area(request.getParameter("txtNombre"));

           try {
                areService.registrarArea(area);
            }catch(Exception ex) {
                //Ir pagina de error
            }
           //Regresar pagina producto_nuevo
          response.sendRedirect("TablasArea.jsp");
    }
    private void modificarArea(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        /*ProductoService proService = new ProductoService();
        String filtro = request.getParameter("txtFiltro");

        Producto producto = new Producto();
        producto.setId(Integer.parseInt(request.getParameter("txtId")));
        producto.setNombre(request.getParameter("txtNombre"));
        producto.setPrecio(Float.parseFloat(request.getParameter("txtPrecio")));
        producto.setStock(Integer.parseInt(request.getParameter("txtStock")));

        proService.modificarProducto(producto);

        String url = "/producto_consultar.isil?txtNombre=" + filtro ;
        getServletContext().getRequestDispatcher(url).forward(request, response);*/
    }

    private void eliminarArea(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*ProductoService proService = new ProductoService();
        int id = Integer.parseInt(request.getParameter("id"));

        proService.eliminarProducto(id);

        String url = "/producto_consultar.isil?txtNombre=" + request.getParameter("txtNombre") ;
        getServletContext().getRequestDispatcher(url).forward(request, response);*/
    }

    private void consultarArea(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                
        AreaService areService = new AreaService();
        String nombre = request.getParameter("txtNombre");

        List<Area> areas = areService.consultarPorNombre(nombre);        

        HttpSession session = request.getSession(true);
        session.setAttribute("areas", areas);

        String url = "xml/areas.jsp";
        response.sendRedirect(url);

    }
}
